package com.example.zjsr.service.impl;

import com.example.zjsr.dao.platform.PlatformMapper;
import com.example.zjsr.domain.Admin.platform.Platform;
import com.example.zjsr.service.PlatformService;
import com.example.zjsr.util.FileUploadAndDownload;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author lvyongwei
 */
@Service
public class PlatformServiceImpl implements PlatformService {

    @Autowired
    private PlatformMapper platformMapper;

    @Autowired
    private FileUploadAndDownload fileUploadAndDownload;

    /**
     * 添加平台信息
     * @param platform
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean insertPlatform(Platform platform, MultipartFile[] files) {
        if(StringUtils.isNotBlank(files[0].getOriginalFilename())){
            String upload = fileUploadAndDownload.upload2(files[0],"platform\\");
            platform.setPlatformIcon(upload);
        }
        if(StringUtils.isNotBlank(files[1].getOriginalFilename())){
            String upload = fileUploadAndDownload.upload2(files[1],"platform\\");
            platform.setPlatformContent(upload);
        }
        return platformMapper.insertPlatform(platform);
    }

    /**
     * 修改平台基本信息
     * @param platform
     * @param file1
     * @param file2
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updatePlatform(Platform platform, MultipartFile file1,MultipartFile file2) {
        /**
         * 根据id查询内容
         */
        Platform platformById = platformMapper.getPlatformById(platform.getPlatformId());
        if(file1!=null && StringUtils.isNotBlank(file1.getOriginalFilename())){
            String upload = fileUploadAndDownload.upload2(file1,"platform\\");
            fileUploadAndDownload.deleteFile(platformById.getPlatformIcon());
            platform.setPlatformIcon(upload);
        }
        if(file2!=null && StringUtils.isNotBlank(file2.getOriginalFilename())){
            String upload = fileUploadAndDownload.upload2(file2,"platform\\");
            fileUploadAndDownload.deleteFile(platformById.getPlatformContent());
            platform.setPlatformContent(upload);
        }
        return platformMapper.updatePlatform(platform);
    }

    /**
     * 获取所有平台信息
     * @return
     */
    @Override
    public List<Platform> getPlatforms() {
        return platformMapper.getPlatforms();
    }

    /**
     * 修改平台的具体信息
     * @param platforms
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updatePlatformDetail(List<Platform> platforms) {
        platforms.forEach(
                platform -> {
                    platformMapper.updatePlatform(platform);
                }
        );
    }
}
